<template>
  <section class="todoapp">
    <ListHead @listAdd="listAdd"></ListHead>
    <ListBody
      :list="list"
      @changeFlag="changeFlag"
      @listDel="listDel"
    ></ListBody>
    <ListFooter 
    :sum='sum'
    :Clearbtn="Clearbtn"
     @delAll="delAll"></ListFooter>
  </section>
</template>

<script>
import ListHead from "./components/ListHead.vue";
import ListBody from "./components/ListBody.vue";
import ListFooter from "./components/ListFooter";
export default {
  components: {
    ListHead,
    ListBody,
    ListFooter,
  },
  data() {
    return {
      list: [
        {
          id: 1,
          flag: false,
          value: "读万卷书",
        },
        {
          id: 2,
          flag: true,
          value: "行万里路",
        },
      ],
    };
  },
  methods: {
    // 改变选中框的flag
    changeFlag(id, flag) {
      this.list.forEach((item) => {
        if (item.id === id) item.flag = flag;
      });
    },

    // 添加事件
    listAdd(value) {
      this.list = [
        ...this.list,
        {
          id: +new Date(),
          flag: false,
          value,
        },
      ];
    },

    //删除事情
    listDel(id) {
      const event = this.list.find((item) => item.id === id);

      if (!event.flag) return alert("无法删除此数据");

      this.list = this.list.filter((item) => item.id !== id);
    },
    //删除全部完成事情
    delAll() {
      this.list = this.list.filter((item) => !item.flag);
    },
  },
  computed: {
    Clearbtn() {
      return this.list.some((item) => item.flag);
    },
    sum(){
      return this.list.filter(item=>!item.flag).length
    }
  },
};
</script>

<style></style>
